package com.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

	private static final Logger log = Logger.getLogger(LoggingAspect.class);

	@Around("traceInvocationPointcut()")
	public Object afficherTrace(final ProceedingJoinPoint jointPoint)
			throws Throwable {

		String nomMethode = jointPoint.getTarget().getClass().getSimpleName() + "."	+ jointPoint.getSignature().getName();
		final Object[] args = jointPoint.getArgs();
		final StringBuffer sb = new StringBuffer();
		sb.append(jointPoint.getSignature().toString());
		sb.append(" avec les parametres : (");
		for (int i = 0; i < args.length; i++) {
			sb.append(args[i]);
			if (i < args.length - 1) {
				sb.append(", ");
			}
		}

		sb.append(")");
		log.info("Debut methode : " + sb);
		Object obj = jointPoint.proceed();
		log.info("Fin methode : " + nomMethode + " retour=" + obj);

		return obj;

	}

	@Pointcut("execution(* com.service.impl.*.*(..))")
	public void traceInvocationPointcut() {

	}
}
